//
//  NextDetailViewController.m
//  我的小汽车
//
//  Created by 杜远洋 on 15-1-18.
//  Copyright (c) 2015年 蓝欧科技. All rights reserved.
//

#import "NextDetailViewController.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "CarDetailTableViewCell.h"
#import "HeaderModel.h"
#import "CellModel.h"
@interface NextDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation NextDetailViewController
- (void)dealloc
{
    [_tableArray release];
    [_tableView release];
    [_headerArray release];
    [super dealloc];
   
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        self.headerArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self .view setBackgroundColor:[UIColor whiteColor]];
   
       [self myConnection];
}

- (void)myConnection
{
    NSString *myUrl = [NSString stringWithFormat:@"http://mi.xcar.com.cn/interface/xcar/GetSeriesInfoByPSeriesNew.php?pseriesId=%ld&ver=5.3.4",self.myCarId];
    [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
    NSURL *url = [NSURL URLWithString:myUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        HeaderModel *model = [[HeaderModel alloc] init];
        [model setValuesForKeysWithDictionary:JSON];
        NSArray *array = [JSON objectForKey:@"subSeries"];
        NSDictionary *dic = [array firstObject];
        model.seriesName = [dic objectForKey:@"seriesName"];
        [self.headerArray addObject:model];
        [model release];
        NSMutableArray *myArray = [dic objectForKey:@"cars"];
        
        for (NSDictionary *myDic in myArray) {
            CellModel *model = [[CellModel alloc] init];
            [model setValuesForKeysWithDictionary:myDic];
            [self.tableArray addObject:model];
            [model release];
        }
        
      [self createMyCarDetail];

        
    
        
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"%@",error);

    }];
    [operation start];
}

- (void)createMyCarDetail
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[CarDetailTableViewCell class] forCellReuseIdentifier:@"cell"];
    UIView *myView = [self myTableHeaderView];
    [self.tableView setTableHeaderView:myView];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    
}
- (UIView *)myTableHeaderView
{
    HeaderModel *model = [self.headerArray firstObject];
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/2)];
    UILabel *firstLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 150, 40)];
    [firstLabel setTextAlignment:NSTextAlignmentCenter];
    [firstLabel setBackgroundColor:[UIColor lightGrayColor]];
    [firstLabel setText:model.bname];
    UILabel *nextLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 101, 60, 40)];
    [nextLabel setBackgroundColor:[UIColor lightGrayColor]];
    [nextLabel setTextAlignment:NSTextAlignmentCenter];
    [nextLabel setText:model.level_name];
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(95, 30, 260, 180)];
    [image setBackgroundColor:[UIColor clearColor]];
    [image setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:nil];
    
    [myView addSubview:image];
    [myView addSubview:firstLabel];
    [myView addSubview:nextLabel];
    
    UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(30, 250, 120, 40)];
    [button1 setTitle:@"问促销价" forState:UIControlStateNormal];
    [button1 setBackgroundColor:[UIColor blueColor]];
    UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(180, 250, 120, 40)];
    [button2 setTitle:@"参数配置表" forState:UIControlStateNormal];
    [button2 setBackgroundColor:[UIColor blueColor]];
    
    [myView addSubview:button2];
    [myView addSubview:button1];
    
    
    return myView;
    
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HeaderModel *model = [self.headerArray firstObject];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 35)];
    [view setBackgroundColor:[UIColor orangeColor]];
    UILabel *left = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 35)];
    [left setBackgroundColor:[UIColor grayColor]];
    [left setText:@"对比"];
    
    UILabel *middle = [[UILabel alloc] initWithFrame:CGRectMake(150, 0, 120, 35)];
    middle.center = view.center;
    [middle setBackgroundColor:[UIColor grayColor]];
    [middle setText:model.seriesName];
    
    [view addSubview:left];
    [view addSubview:middle];
    
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 35;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tableArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CarDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    CellModel *model = [self.tableArray objectAtIndex:indexPath.row];
    cell.engine.text = model.engine;
    cell.transmission.text = model.transmission;
    cell.guidePrice.text = model.guidePrice;
    cell.name.text = model.name;
    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
